package com.anycc.cloud.common.dto.query;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.ArrayList;
import java.util.List;

/**
 * paging request
 */
@Data
public class PagingRequest {
    private int page;
    private int size;
    private List<OrderBy> orders;

    public PagingRequest() {
        this.page = 0;
        this.size = 10;
        this.orders = new ArrayList<>();
    }

    public PagingRequest(int page, int size) {
        this.page = 0;
        this.size = 10;
        this.orders = new ArrayList<>();
        this.setPage(page);
        this.setSize(size);
    }


    public PagingRequest(int page, int size, List<OrderBy> orders) {
        this(page, size);
        if (orders != null && !orders.isEmpty()) {
            this.orders.addAll(orders);
        }
    }


    public void setPage(int page) {
        if (page < 0) {
            throw new IllegalArgumentException("Page index must not be less than zero");
        } else {
            this.page = page;
        }
    }

    public void setSize(int size) {
        if (size < 1) {
            throw new IllegalArgumentException("Page size must not be less than one!");
        } else {
            this.size = size;
        }
    }

    public int getOffset() {
        return page * size;
    }

}
